#!/bin/bash

#readme 
#Aaron 461177348@qq.com 20181119
#install Advanced installer for msi build.
#install subversion for windows.

function echo_error()
{
	err_msg=$1
    echo -e "\033[31m $err_msg \033[0m"
}

function echo_info()
{
	err_msg=$1
    echo -e "\033[33m $err_msg \033[0m"
}

if [ $# -ne 2 ]
then
	echo "Usage: ./autobuild_ly svn_name svn_pwd"
	exit 1
fi

echo_info "准备数据"
USERNAME=$1
PASSWORD=$2

SOURCE_URL="https://10.88.1.11/svn/wtgeo/WtgeoProducts/"
TAG_URL="https://10.88.1.11/svn/wtgeo/Tag/WtgeoProducts/"
RELEASE_URL="https://10.88.1.11/svn/wtgeo/release/WtgeoProducts/"

SHARE_USER_NAME="share"
SHARE_USER_PWD="Ss123456"

echo_info "获取发布版本号"
svn list $TAG_URL --username=$USERNAME --password=$PASSWORD > svnlist
line=`cat svnlist|wc -l`
a=${line}"p"
temp1=$(sed -n $a ${Project}svnlist) 
read -r -a  words <<< "$temp1"
IFS=_
read -r -a ff <<< "${words[0]}"
Dvar=`date +%Y-%m-%d`
IFS=
NUMBER=`expr ${ff[1]} + 1`
NUMBER=$(printf '%05d' $NUMBER)
rm svnlist

VERSION="V3.0.$NUMBER"
PRODUCT_VERSION="3.0.$NUMBER.0"
FILE_VERSION=3,0,$NUMBER,0
LABEL="build_"$NUMBER"_"${Dvar}
echo $VERSION
echo $LABEL

# echo "定义发布版本目录"
WMPS_DIR="wmps"
DPS="DPS"
DIS="DIS"
RMS="RMS"
PCS="PCS"
SERVER_DIR="Server"
DATA_ACQUIRE="DataAcquire"
HTTP_SERVER="HttpServer"
WMPS_CMD_TOOL="WmpsCmdTool"
RESOURCE_DIR="Resource"
CONFIG_DIR="config"
IMAGES_DIR="images"
CONFIGURATION_DIR="Configuration"
TIMEGRID_DIR="timegrid"

# echo "创建目录"
# BUILD_DATE=`date +%Y%m%d`
# RELEASE_DIR=$LABEL"/"$BUILD_DATE"_"$VERSION
# mkdir -p $RELEASE_DIR
# cd $RELEASE_DIR
# mkdir -p $CONFIG_DIR"/"$TIMEGRID_DIR $WMPS_DIR $IMAGES_DIR
# cd $WMPS_DIR
# mkdir $DPS $DIS $RMS $SERVER_DIR
# cd $DPS
# mkdir $CONFIG_DIR $IMAGES_DIR
# cd ..
# cd $DIS
# mkdir $CONFIG_DIR $IMAGES_DIR
# cd ..
# cd $RMS
# mkdir $CONFIG_DIR $IMAGES_DIR
# cd ..
# cd $SERVER_DIR
# mkdir -p $RESOURCE_DIR"/"$CONFIGURATION_DIR $RESOURCE_DIR"/"$TIMEGRID_DIR $DATA_ACQUIRE $HTTP_SERVER $WMPS_CMD_TOOL
# cd ../../../../

cd ../..
cur_dir=`pwd`
echo  "cd $cur_dir."

echo_info "签出代码"
if [ ! -d "WtgeoProducts" ]
then
#svn checkout $SOURCE_URL --username=$USERNAME --password=$PASSWORD
	if [ $? != 0 ]
	then 
		echo_error "Checkout code failed!!!" 
		exit 1
	fi
else
	cd WtgeoProducts
	svn update
	cd .. 
fi


echo_info "修改version.h"
cd WtgeoProducts/System/WMPS/Common/WmpsInc
VDATE=`date +%Y-%m-%d`
VTIME=`date +%H:%M:%S`
echo "alter version.h file"
if [ -f "./version.h" ]
then 
	rm -rf ./version.h
	if [ $? != 0 ]
	then
      echo_error  "rm local version.h error!!!"
	exit 1
	fi
fi
touch version.h
if [ -f "./version.h" ]
then
	echo "//#############################################################################" >> version.h
	echo "//# Generated by autobuild ${VDATE} ${VTIME}" >> version.h
	echo "//# Do not modify this file" >> version.h
	echo "//#############################################################################" >> version.h
	echo "#ifndef __VERSION_H__" >> version.h
	echo -e "#define __VERSION_H__\n" >> version.h

	echo "#define VERSION       \"$VERSION\"" >> version.h
	echo "#define VER_PRODUCTVERSION_STR       \"$PRODUCT_VERSION\"" >> version.h
	echo "#define VER_FILE_VERSION       $FILE_VERSION" >> version.h
	echo -e "#define BUILD_TIME    \"${VDATE} ${VTIME}\"\n" >> version.h

	echo "#endif //__VERSION_H__" >> version.h
else
        echo_error  "create version.h error!!!"
	exit 1
fi

cd ../../APP/

echo_info "编译DPS"
cd $DPS
cur_dir=`pwd`
echo  "cd $cur_dir."
qmake.exe $DPS.pro -r -spec win32-g++ "CONFIG+=release" >> dps.qmake_log
	if [ $? != 0 ]
	then
	       	echo_error "qmake $DPS.pro failed!!! Detail in dps.qmake_log"
		exit 1
	fi
echo  "qmake $DPS.pro success."

mingw32-make -release >> dps.make_log
	if [ $? != 0 ]
	then
	       	echo_error  "Do $DPS mingw32-make -release failed!!!  Detail in dps.make_log"
		exit 1
	fi
echo  "make $DPS.pro success."

echo_info "编译DIS"
cd ../$DIS
cur_dir=`pwd`
echo  "cd $cur_dir."
qmake.exe $DIS.pro -r -spec win32-g++ "CONFIG+=release" >> dis.qmake_log
	if [ $? != 0 ]
	then
	       	echo_error  "qmake $DIS.pro failed!!! Detail in dis.qmake_log"
		exit 1
	fi
echo  "qmake $DIS.pro success."

mingw32-make -release >> dis.make_log
	if [ $? != 0 ]
	then
	       	echo_error  "Do $DIS mingw32-make -release failed!!! Detail in dis.make_log"
		exit 1
	fi
echo  "make $DIS.pro success."

echo_info "编译RMS"
cd ../$RMS
cur_dir=`pwd`
echo  "cd $cur_dir."
qmake.exe $RMS.pro -r -spec win32-g++ "CONFIG+=release" >> rms.qmake_log
	if [ $? != 0 ]
	then
	       	echo_error  "qmake $RMS.pro failed!!! Detail in rms.qmake_log"
		exit 1
	fi
echo  "qmake $RMS.pro success."
mingw32-make -release >> rms.make_log
	if [ $? != 0 ]
	then
	       	echo_error  "Do $RMS mingw32-make -release failed!!! Detail in rms.make_log" 
		exit 1
	fi
echo  "make $RMS.pro success."

echo_info "编译HTTP_SERVER"
cd ../$HTTP_SERVER
cur_dir=`pwd`
echo  "cd $cur_dir."
qmake.exe $HTTP_SERVER.pro -r -spec win32-g++ "CONFIG+=release" >> http_server.qmake_log
	if [ $? != 0 ]
	then
	       	echo_error  "qmake $HTTP_SERVER.pro failed!!! Detail in http_server.qmake_log"
		exit 1
	fi
echo  "qmake $HTTP_SERVER.pro success."
mingw32-make -release >> http_server.make_log
	if [ $? != 0 ]
	then
	       	echo_error  "Do $HTTP_SERVER mingw32-make -release failed!!!  Detail in http_server.make_log" 
		exit 1
	fi
echo  "make $HTTP_SERVER.pro success."

echo_info "编译DATA_ACQUIRE"
cd ../$DATA_ACQUIRE
cur_dir=`pwd`
echo  "cd $cur_dir."
qmake.exe $DATA_ACQUIRE.pro -r -spec win32-g++ "CONFIG+=release" >> data_acquie.qmake_log
	if [ $? != 0 ]
	then
	       	echo_error  "qmake $DATA_ACQUIRE.pro failed!!! Detail in data_acquie.qmake_log"
		exit 1
	fi
echo  "qmake $DATA_ACQUIRE.pro success."

mingw32-make -release >> data_acquie.make_log
	if [ $? != 0 ]
	then
	       	echo_error  "Do $DATA_ACQUIRE mingw32-make -release failed!!! Detail in data_acquie.make_log"
		exit 1
	fi
echo  "make $DATA_ACQUIRE.pro success."

echo_info "编译WMPS_CMD_TOOL"
cd ../$WMPS_CMD_TOOL
cur_dir=`pwd`
echo  "cd $cur_dir."
qmake.exe $WMPS_CMD_TOOL.pro -r -spec win32-g++ "CONFIG+=release" >> wmps_cmd_tool.qmake_log
	if [ $? != 0 ]
	then
	       	echo_error  "qmake $WMPS_CMD_TOOL.pro failed!!! Detail in wmps_cmd_tool.qmake_log"
		exit 1
	fi
echo  "qmake $WMPS_CMD_TOOL.pro success."
mingw32-make -release >> wmps_cmd_tool.make_log
	if [ $? != 0 ]
	then
	       	echo_error  "Do $WMPS_CMD_TOOL mingw32-make -release failed!!! Detail in wmps_cmd_tool.make_log"
		exit 1
	fi
echo  "make $WMPS_CMD_TOOL.pro success."

echo_info "编译PCS"
cd ../PCS
cur_dir=`pwd`
echo  "cd $cur_dir."

qmake.exe $PCS.pro -r -spec win32-g++ "CONFIG+=release" >> pcs.qmake_log
	if [ $? != 0 ]
	then
	       	echo_error  "qmake $PCS.pro failed!!! Detail in pcs.qmake_log"
		exit 1
	fi
echo  "qmake $PCS.pro success."

mingw32-make -release >> pcs.make_log
	if [ $? != 0 ]
	then
	       	echo_error  "Do $PCS.pro mingw32-make -release failed!!!  Detail in pcs.make_log"
		exit 1
	fi
echo  "make $PCS.pro success."

cd ../../../../Build
cur_dir=`pwd`
echo  "cd $cur_dir."

./advanced_installer_build.bat

echo_info "msi build success."